##############################################################################
#REPLICATION MATERIALS FOR 
#Show Me Your Friends: A Survey Experiment on the Effect of Coalition Signals
#Albert Falc�-Gimeno and Jordi Mu�oz
#Department of Political Science, University of Barcelona
#E-mail: afalcogimeno@ub.edu - jordi.munoz@ub.edu
##############################################################################

#Install packages (if needed):
#install.packages("haven")
#install.packages("readstata13")
#install.packages("mediation")

#Load packages
library(foreign)
library(haven)
library(readstata13)
library(mediation)

#Set working directory
setwd("")



####################
#Table 2: Mediation#
####################

#Coalition Signal: CS-PSOE

set.seed(1)

data <- read.dta13("ABBAES_70505_20150518_run.dta") #this has to be called after running the replication file in Stata (and the database with the recoded variables has been saved)
data$P14_1.c <- as.numeric(data$P14_1)-1
data$P14_3.c <- as.numeric(data$P14_3)-1
data$polint.c <- as.numeric(data$polint)
data <- subset(data,right_cs==0)

m.cspsoe <- lm(dist_cs_sq ~ exp_cspsoe + exp_cspp + exp_csro + exp_psoeleft + exp_psoecs + exp_psoero,data=data)

o.cspsoe <- lm(P14_1.c ~ exp_cspsoe + dist_cs_sq + exp_cspp + exp_csro + exp_psoeleft + exp_psoecs + exp_psoero +
                 close_cs + sophis + polint.c + education + female + age + agesq, data=data)

id.cspsoe.leftcs <- mediate(m.cspsoe, o.cspsoe, treat = "exp_cspsoe", mediator = "dist_cs_sq", conf.level = .95)


#Coalition Signal: CS-PP

set.seed(1)

data <- read.dta13("ABBAES_70505_20150518_run.dta") #this has to be called after running the replication file in Stata (and the database with the recoded variables has been saved)
data$P14_1.c <- as.numeric(data$P14_1)-1
data$P14_3.c <- as.numeric(data$P14_3)-1
data$polint.c <- as.numeric(data$polint)
data <- subset(data,right_cs==1)

m.cspp <- lm(dist_cs_sq ~ exp_cspp + exp_cspsoe + exp_csro + exp_psoeleft + exp_psoecs + exp_psoero,data=data)

o.cspp <- lm(P14_1.c ~ exp_cspp + dist_cs_sq + exp_cspsoe + exp_csro + exp_psoeleft + exp_psoecs + exp_psoero +
               close_cs + sophis + polint.c + education + female + age + agesq, data=data)

id.cspp.rightcs <- mediate(m.cspp, o.cspp, treat = "exp_cspp", mediator = "dist_cs_sq", conf.level = .95)


#Coalition Signal: PSOE-LEFT

set.seed(1)

data <- read.dta13("ABBAES_70505_20150518_run.dta") #this has to be called after running the replication file in Stata (and the database with the recoded variables has been saved)
data$P14_1.c <- as.numeric(data$P14_1)-1
data$P14_3.c <- as.numeric(data$P14_3)-1
data$polint.c <- as.numeric(data$polint)
data <- subset(data,right_psoe==0)

m.psoeleft <- lm(dist_psoe_sq ~ exp_psoeleft + exp_cspsoe + exp_csro + exp_cspp + exp_psoecs + exp_psoero,data=data)

o.psoeleft <- lm(P14_3.c ~ exp_psoeleft + dist_psoe_sq + exp_cspsoe + exp_csro + exp_cspp + exp_psoecs + exp_psoero +
                   close_cs + sophis + polint.c + education + female + age + agesq, data=data)

id.psoeleft.leftpsoe <- mediate(m.psoeleft, o.psoeleft, treat = "exp_psoeleft", mediator = "dist_psoe_sq", conf.level = .95)


#Coalition Signal: PSOE-CS

set.seed(1)

data <- read.dta13("ABBAES_70505_20150518_run.dta") #this has to be called after running the replication file in Stata (and the database with the recoded variables has been saved)
data$P14_1.c <- as.numeric(data$P14_1)-1
data$P14_3.c <- as.numeric(data$P14_3)-1
data$polint.c <- as.numeric(data$polint)
data <- subset(data,right_psoe==1)

m.psoecs <- lm(dist_psoe_sq ~ exp_psoecs + exp_cspsoe + exp_csro + exp_cspp + exp_psoeleft + exp_psoero,data=data)

o.psoecs <- lm(P14_3.c ~ exp_psoecs + dist_psoe_sq + exp_cspsoe + exp_csro + exp_cspp + exp_psoeleft + exp_psoero +
                 close_psoe + sophis + polint.c + education + female + age + agesq, data=data)

id.psoecs.rightpsoe <- mediate(m.psoecs, o.psoecs, treat = "exp_psoecs", mediator = "dist_psoe_sq", conf.level = .95)


#Summary of estimates (Table 2)
summary(id.cspsoe.leftcs)
summary(id.cspp.rightcs)
summary(id.psoeleft.leftpsoe)
summary(id.psoecs.rightpsoe)



########################
#Appendix E2, Table E.5#
########################


#Coalition Signal: CS-PSOE

set.seed(1)

data <- read.dta13("ABBAES_70505_20150518_run.dta") #this has to be called after running the replication file in Stata (and the database with the recoded variables has been saved)
data$P14_1.c <- as.numeric(data$P14_1)-1
data$P14_3.c <- as.numeric(data$P14_3)-1
data$polint.c <- as.numeric(data$polint)
data <- subset(data,right_cs==0)

m.cspsoe <- lm(dist_cs_sq ~ exp_cspsoe + exp_cspp + exp_csro + exp_psoeleft + exp_psoecs + exp_psoero +
                 close_cs + sophis + polint.c + education + female + age + agesq,data=data)

o.cspsoe <- lm(P14_1.c ~ exp_cspsoe + dist_cs_sq + exp_cspp + exp_csro + exp_psoeleft + exp_psoecs + exp_psoero +
                 close_cs + sophis + polint.c + education + female + age + agesq, data=data)

f.id.cspsoe.leftcs <- mediate(m.cspsoe, o.cspsoe, treat = "exp_cspsoe", mediator = "dist_cs_sq", conf.level = .95)


#Coalition Signal: CS-PP

set.seed(1)

data <- read.dta13("ABBAES_70505_20150518_run.dta") #this has to be called after running the replication file in Stata (and the database with the recoded variables has been saved)
data$P14_1.c <- as.numeric(data$P14_1)-1
data$P14_3.c <- as.numeric(data$P14_3)-1
data$polint.c <- as.numeric(data$polint)
data <- subset(data,right_cs==1)

m.cspp <- lm(dist_cs_sq ~ exp_cspp + exp_cspsoe + exp_csro + exp_psoeleft + exp_psoecs + exp_psoero +
               close_cs + sophis + polint.c + education + female + age + agesq,data=data)

o.cspp <- lm(P14_1.c ~ exp_cspp + dist_cs_sq + exp_cspsoe + exp_csro + exp_psoeleft + exp_psoecs + exp_psoero +
               close_cs + sophis + polint.c + education + female + age + agesq, data=data)

f.id.cspp.rightcs <- mediate(m.cspp, o.cspp, treat = "exp_cspp", mediator = "dist_cs_sq", conf.level = .95)


#Coalition Signal: PSOE-LEFT

set.seed(1)

data <- read.dta13("ABBAES_70505_20150518_run.dta") #this has to be called after running the replication file in Stata (and the database with the recoded variables has been saved)
data$P14_1.c <- as.numeric(data$P14_1)-1
data$P14_3.c <- as.numeric(data$P14_3)-1
data$polint.c <- as.numeric(data$polint)
data <- subset(data,right_psoe==0)

m.psoeleft <- lm(dist_psoe_sq ~ exp_psoeleft + exp_cspsoe + exp_csro + exp_cspp + exp_psoecs + exp_psoero +
                   close_psoe + sophis + polint.c + education + female + age + agesq,data=data)

o.psoeleft <- lm(P14_3.c ~ exp_psoeleft + dist_psoe_sq + exp_cspsoe + exp_csro + exp_cspp + exp_psoecs + exp_psoero +
                   close_psoe + sophis + polint.c + education + female + age + agesq, data=data)

f.id.psoeleft.leftpsoe <- mediate(m.psoeleft, o.psoeleft, treat = "exp_psoeleft", mediator = "dist_psoe_sq", conf.level = .95)


#PSOE-CS

set.seed(1)

data <- read.dta13("ABBAES_70505_20150518_run.dta") #this has to be called after running the replication file in Stata (and the database with the recoded variables has been saved)
data$P14_1.c <- as.numeric(data$P14_1)-1
data$P14_3.c <- as.numeric(data$P14_3)-1
data$polint.c <- as.numeric(data$polint)
data <- subset(data,right_psoe==1)

m.psoecs <- lm(dist_psoe_sq ~ exp_psoecs + exp_cspsoe + exp_csro + exp_cspp + exp_psoeleft + exp_psoero +
                 close_psoe + sophis + polint.c + education + female + age + agesq,data=data)

o.psoecs <- lm(P14_3.c ~ exp_psoecs + dist_psoe_sq + exp_cspsoe + exp_csro + exp_cspp + exp_psoeleft + exp_psoero +
                 close_psoe + sophis + polint.c + education + female + age + agesq, data=data)

f.id.psoecs.rightpsoe <- mediate(m.psoecs, o.psoecs, treat = "exp_psoecs", mediator = "dist_psoe_sq", conf.level = .95)


#Summary of estimates (Table E.5)
summary(f.id.cspsoe.leftcs)
summary(f.id.cspp.rightcs)
summary(f.id.psoeleft.leftpsoe)
summary(f.id.psoecs.rightpsoe)



#########################
#Appendix E2, Figure E.1#
#########################

set.seed(1)

data <- read.dta13("ABBAES_70505_20150518_run.dta") #this has to be called after running the replication file in Stata (and the database with the recoded variables has been saved)
data$P14_1.c <- as.numeric(data$P14_1)-1
data$P14_3.c <- as.numeric(data$P14_3)-1
data$polint.c <- as.numeric(data$polint)
data <- subset(data,right_cs==1)

m.cspp <- lm(dist_cs_sq ~ exp_cspp + exp_cspsoe + exp_csro + exp_psoeleft + exp_psoecs + exp_psoero,data=data)

o.cspp <- lm(P14_1.c ~ exp_cspp + dist_cs_sq + exp_cspsoe + exp_csro + exp_psoeleft + exp_psoecs + exp_psoero +
               close_cs + sophis + polint.c + education + female + age + agesq, data=data)

id.cspp.rightcs <- mediate(m.cspp, o.cspp, treat = "exp_cspp", mediator = "dist_cs_sq", conf.level = .90)

s.id.cspp.rightcs <- medsens(id.cspp.rightcs , rho.by = 0.1 , effect.type = "indirect" , sims = 1000)

plot(s.id.cspp.rightcs, "rho")
abline(v=-0.32,col="red",lty=1)
title(sub=expression(paste("Distance (", d[j], "): Lib. (right-lean.)")), cex.sub=1.5)
dev.copy(pdf,'s_id_cspp_rightcs.pdf')
dev.off()
